home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7903 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.2 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Using regexec for multiple patterns. How???
  5. Date: Thu, 29 Feb 96 13:28:40 GMT
  6. Organization: none
  7. Distribution: all
  8. Message-ID: <825600520snz@genesis.demon.co.uk>
  9. References: <4h2pdj$fui@mo6.rc.tudelft.nl>
  10. Reply-To: fred@genesis.demon.co.uk
  11. X-NNTP-Posting-Host: genesis.demon.co.uk
  12. X-Newsreader: Demon Internet Simple News v1.27
  13. X-Mail2News-Path: genesis.demon.co.uk
  14.  
  15. In article <4h2pdj$fui@mo6.rc.tudelft.nl>
  16.            koen@lr46pstn.lr.tudelft.nl "Koen D'Hondt" writes:
  17.  
  18. >Hi All,
  19. >
  20. >I've been experimenting with regcomp and friends to do pattern-matching in
  21. >texts, but I've run into a few problems trying to search for multiple 
  22. >occurances of a search pattern. The regcomp and regexec are the GNU stuff. 
  23. >The system I'm using is Linux 1.3.63.
  24.  
  25. comp.lang.c isn't the place to discuss regcomp since it isn't part of the
  26. C language. Try a GNU or Linux newsgroup or possibly comp.unix.programmer.
  27.  
  28. ...
  29.  
  30. >/* this causes a SIGSEGV when count > 1 */
  31. >    for(i = 0; i < count && pmatch[i].rm_so != -1; i++)
  32. >    {
  33. >        bzero(buf,128); 
  34.  
  35. bzero isn't portable even among Unix systems. The ANSI function is called
  36. memset.
  37.  
  38. >        chPtr = &search_text[pmatch[i].rm_so];
  39. >        strncpy(buf, chPtr, pmatch[i].rm_eo - pmatch[i].rm_so);
  40. >        strcat(buf,"\0");
  41.  
  42. This is a true horror. As you seem to be aware strncpy doesn't necessarily
  43. null terminate the result string. However strcat *requires* a properly
  44. terminated string (that's the only way it can find where the end of the string
  45. is). strcat(buf,"\0"); will either do nothing if it is given a properly
  46. formatted string or produce undefined behaviour (e.g. possibly crash) if
  47. not. Write the last 2 lines as:
  48.  
  49.          buf[0] = '\0';
  50.          strncat(buf, chPtr, pmatch[i].rm_eo - pmatch[i].rm_so);
  51.  
  52. >        printf("%i: start at %i, end at %i (%s)\n",
  53. >            i, pmatch[i].rm_so, pmatch[i].rm_eo, buf);  
  54.  
  55. While %i is supported by ANSI C %d is more commonly used since it is portable
  56. to older C compilers.
  57.  
  58. -- 
  59. -----------------------------------------
  60. Lawrence Kirby | fred@genesis.demon.co.uk
  61. Wilts, England | 70734.126@compuserve.com
  62. -----------------------------------------
  63.